﻿using community.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;

namespace community.Controllers
{
    [Route("api/permissions")]
    [ApiController]
    public class PermissionsController : ControllerBase
    {
        private static List<Permission> permissions = new List<Permission>();

        // 添加权限
        [HttpPost]
        public IActionResult AddPermission([FromBody] Permission newPermission)
        {
            newPermission.Id = permissions.Count + 1;
            permissions.Add(newPermission);
            return CreatedAtAction(nameof(GetPermissionById), new { id = newPermission.Id }, newPermission);
        }

        // 获取所有权限
        [HttpGet]
        public IActionResult GetAllPermissions()
        {
            return Ok(permissions);
        }

        // 根据ID查询权限
        [HttpGet("{id}")]
        public IActionResult GetPermissionById(int id)
        {
            var permission = permissions.FirstOrDefault(p => p.Id == id);
            if (permission == null)
            {
                return NotFound();
            }
            return Ok(permission);
        }

        // 更新权限信息
        [HttpPut("{id}")]
        public IActionResult UpdatePermission(int id, [FromBody] Permission updatedPermission)
        {
            var permission = permissions.FirstOrDefault(p => p.Id == id);
            if (permission == null)
            {
                return NotFound();
            }

            permission.Name = updatedPermission.Name;
            permission.Description = updatedPermission.Description;

            return NoContent();
        }

        // 删除权限
        [HttpDelete("{id}")]
        public IActionResult DeletePermission(int id)
        {
            var permission = permissions.FirstOrDefault(p => p.Id == id);
            if (permission == null)
            {
                return NotFound();
            }

            permissions.Remove(permission);
            return NoContent();
        }
    }
}
